﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ZooKeeperNet;

namespace DistributedLock
{
    public class ExclusiveWatcher : IWatcher
    {
        public void Process(WatchedEvent @event)
        {
            if (@event.State == KeeperState.Expired || @event.State == KeeperState.Disconnected ||
                @event.State == KeeperState.NoSyncConnected || @event.State == KeeperState.Unknown)
            {
                Console.WriteLine("delete");
                ZKServer.DeleteNode(@event.Path);
            }

            if (@event.Type == EventType.NodeDeleted)
            {
                var parentNode = ZKUtility.GetParentNode(@event.Path);
                var lowestNode = ZKUtility.GetLowestNode(parentNode);
                Mutex.Continue(lowestNode);
            }
        }
    }
}
